using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CodeComment.NET
{
    public partial class CommentsEntryForm : Form
    {
        public CommentsEntryForm()
        {
            InitializeComponent();
        }

        public CommentsEntryForm(string projectName, string projectItemName, string message, string localFilePath) : this()
        {
            this.txtSelectedProject.Text = projectName;
            this.txtSelectedFile.Text = projectItemName;
            this.txtSelection.Text = message;
            this.txtLocalFilePath.Text = localFilePath;

            this.txtCommentsFile.Text = SettingsHandler.GetCommentsOutputFilenamePath();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnChooseCommentsFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.AddExtension = false;
            openFileDlg.CheckPathExists = true;
            openFileDlg.CheckFileExists = false;
            openFileDlg.DefaultExt = "txt";
            openFileDlg.Multiselect = false;
            openFileDlg.RestoreDirectory = false;
            openFileDlg.SupportMultiDottedExtensions = true;
            openFileDlg.Title = "Choose code comments output file...";

            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                this.txtCommentsFile.Text = openFileDlg.FileName;
            }
        }

        private void CommentsEntryForm_Load(object sender, EventArgs e)
        {
            this.txtComments.Focus();
        }

        private void btnStoreComments_Click(object sender, EventArgs e)
        {
            if (this.StoreNewComment())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private bool StoreNewComment()
        {
            bool retVal = false;

            if (String.IsNullOrEmpty(this.txtCommentsFile.Text))
            {
                this.ShowError("Output file must be selected before comments can be stored!");
            }
            else
            {
                try
                {
                    string newComment = this.GetNewCommentFormated();
                    FileMode fileMode;
                    if (this.rbtnAppendToFile.Checked)
                    {
                        fileMode = FileMode.Append;
                    }
                    else
                    {
                        fileMode = FileMode.Create;
                    }
                    using (FileStream fs = new FileStream(this.txtCommentsFile.Text, fileMode, FileAccess.Write, FileShare.Read))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.Write(newComment);
                            retVal = true;

                            //Try to store last used values into configuration section of registry
                            SettingsHandler.StoreCommentsOutputFilenamePath(this.txtCommentsFile.Text);
                        }
                    }
                }
                catch (Exception exc)
                {
                    this.ShowError(exc.Message);
                }
            }

            return retVal;
        }

        private void ShowError(string errorMessage)
        {
            MessageBox.Show(errorMessage, "CodeComment.NET - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private string GetNewCommentFormated()
        {
            StringBuilder newComment = new StringBuilder();

            newComment.Append(Environment.NewLine);
            newComment.Append(Environment.NewLine);
            if (this.cboxIncludeCurrentDateInOutput.Checked)
            {
                newComment.Append(Environment.NewLine);
                string date = String.Format("[{0}]", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                newComment.Append(date);
            }
            if (this.cboxIncludeCommentsInOutput.Checked)
            {
                newComment.Append(Environment.NewLine);
                newComment.Append("Comments: ");
                newComment.Append(this.txtComments.Text);
            }
            if (this.cboxIncludeSelectionInOutput.Checked)
            {
                newComment.Append(Environment.NewLine);
                newComment.Append("Selection: ");
                newComment.Append(Environment.NewLine);
                newComment.Append(this.txtSelection.Text);
                newComment.Append(Environment.NewLine);
            }
            if (this.cboxIncludeFilenameInOutput.Checked)
            {
                newComment.Append(Environment.NewLine);
                newComment.Append("File: ");
                newComment.Append(this.txtSelectedFile.Text);
            }
            if (this.cboxIncludeProjectInOutput.Checked)
            {
                newComment.Append(Environment.NewLine);
                newComment.Append("Project: ");
                newComment.Append(this.txtSelectedProject.Text);
            }
            if (this.cBoxIncludeLocalPathInOutput.Checked)
            {
                newComment.Append(Environment.NewLine);
                newComment.Append("Local path: ");
                newComment.Append(this.txtLocalFilePath.Text);
            }

            return newComment.ToString();
        }
    }
}